<template>
  <div class="ybl x">
    <zbl :buttons="buttons" :tree="tree" @change="change" style="z-index:1" />
    <depositoryA :form="form" :fshow="show" @change="change" />
    <div class="ybl">
      <h2 v-text="'库存预览'" class="m10px" />
      <div class="x">
        <el-input placeholder="请输入物料号" v-model="uid" />
        <el-button v-text="'查找'" type="primary" size="mini" @click="ss" />
      </div>
      <div class="fg1 wf y">
        <tableIO :value="items" :fun="0" />
      </div>
    </div>
  </div>
</template>

<script>
import zbl from "../components/zbl";
import depositoryA from "../components/create/depositoryA.vue";
import tableIO from "./bill/tableIO";
import XLSX from "xlsx";

export default {
  components: { zbl, depositoryA, tableIO },
  watch: {
    $gd(a) {
      // console.log(this.$cp(a));
      this.setTree(a);
    }
  },
  data() {
    return {
      buttons: [
        {
          id: 1,
          name: "新增顶级",
          Attributes: { size: "mini", type: "primary" },
          style: "",
          class: "m10px"
        },
        {
          id: 2,
          name: "新增下级",
          Attributes: { size: "mini", type: "primary", disabled: true },
          style: "",
          class: "m10px"
        },
        {
          id: 3,
          name: "新增平级",
          Attributes: { size: "mini", type: "primary", disabled: true },
          style: "",
          class: "m10px"
        },
        {
          id: 4,
          name: "修改",
          Attributes: { size: "mini", type: "primary", disabled: true },
          style: "",
          class: "m10px"
        },
        {
          id: 5,
          name: "导出全部",
          Attributes: { size: "mini", type: "danger", plain: true },
          style: "",
          class: "m10px"
        },
        {
          id: 6,
          name: "导出选中",
          Attributes: {
            size: "mini",
            type: "success",
            plain: true,
            disabled: true
          },
          style: "",
          class: "m10px"
        }
      ],
      show: false,
      tree: [],
      form: {},
      defForm: { id: 0, pid: 0, userType: 0, name: "", other: { remarks: "" } },
      current: {},
      items: [],
      uid: "",
      is: []
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
    this.setTree();
    this.form = this.$cp(this.defForm);
    this.current = this.$cp(this.defForm);
  },
  methods: {
    getHeight() {
      this.dhf = "min-height: " + (window.innerHeight - 157) + "px;";
    },
    setTree() {
      // console.log(this.$cp(this.$gd.depository));
      this.tree = this.$gd.depository.arr2tree();
    },
    ss() {
      let items = this.is;
      if (!items.length) {
        this.is = this.$cp(this.items);
        items = this.items;
      }
      // console.log(this.$cp(items));
      this.items = items.filter(a => {
        // console.log(a.uid.indexOf(this.uid));
        return a.uid && a.uid.indexOf(this.uid) >= 0;
      });
    },
    change(a, b, c) {
      let tmp = {};
      let t = {};
      if (a > 0 && a < 4) this.form = this.$cp(this.defForm);
      switch (a) {
        case 1: // 新增顶级
          // 前面已经赋值.
          break;
        case 2: // 新增下级
          this.form.pid = this.current.id;
          break;
        case 3: // 新增平级
          this.form.pid = this.current.pid;
          break;
        case 4: // 修改
          this.form = this.current;
          break;
        case 5: // 导出全部
          this.export(0);
          break;
        case 6: // 导出选中
          this.export(this.current.id);
          break;
        case 101: // 选中
          this.is = [];
          if (b) this.current = b;
          // if (this.current == b) return;
          this.buttons = this.buttons.map(a => {
            if (a.Attributes.disabled) a.Attributes.disabled = false;
            return a;
          });
          tmp = this.$gd.stock;
          tmp = tmp.reduce((a, b) => {
            return a + b.sum;
          }, 0);
          tmp = this.$gd.stock.filter(a => {
            return a.depository == b.id;
          });
          this.items = this.$cp(tmp);
          this.items.map(a => {
            a.sum = a.stock;
            if (!a.projectName) {
              a.projectName = this.$gd.project.getItem({
                id: this.$gd.project.getItem({ id: a.project }).pid
              }).name;
            }
          });
          // console.log(this.$cp(this.items));
          break;
        case 201: // 新增/修改  完毕
          if (c) {
            this.$gd.depository.push(b);
            this.setTree();
          }
          break;
        case "tree": // tree被改变
          this.$gd.depository.v2item(b).pid = c;
          this.setTree();
          this.$post({
            cmd: 1,
            val: {
              token: sessionStorage.token,
              table: "depository",
              pid: c,
              where: { id: b }
            }
          });
          break;

        default:
          break;
      }
      if (a > 0 && a < 5) this.show = !this.show;
    },
    export(val, FileName = "导出库存.xlsx") {
      // if (!val.length) return;
      // console.log(val);
      // return;
      let data = {};
      let SheetNames = [];
      this.$cp(
        this.$gd.stock.filter(i => {
          if (val) return i.depository === val;
          else return i.depository;
        })
      ).map(a => {
        let t = this.$gd.depository.getItem({ id: a.depository }).name;
        if (typeof data[t] == "undefined") {
          data[t] = [
            [
              "存货id",
              "物料id",
              "物料编号",
              "物料名称",
              "物料型号",
              "预计需求",
              "已到",
              "已用",
              "库存",
              "工序",
              "项目",
              "列次"
            ]
          ];
          SheetNames.push(t);
        }
        let project = this.$gd.project.getItem({ id: a.project });
        let sum = this.$gd.stock.getItem({
          depository: 0,
          project: a.project,
          item: a.item,
          process: a.process
        }).sum;
        data[t].push([
          a.id,
          a.item,
          a.uid,
          a.name,
          a.model,
          sum ? sum : "无预计需求",
          a.arrived,
          a.use,
          a.stock,
          this.$gd.process.getItem({ id: a.process }).name,
          this.$gd.project.getItem({ id: project.pid }).name,
          project.name
        ]);
      });
      SheetNames.map(a => {
        data[a] = XLSX.utils.aoa_to_sheet(data[a]);
      });
      let file = XLSX.utils.book_new();
      file["SheetNames"] = SheetNames;
      file["Sheets"] = data;
      XLSX.writeFile(file, FileName);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
